﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Abp.IO;

namespace ARchGL.Platform.IO
{
    public static class AppFileHelper
    {
        #region 变量

        /// <summary>
        /// 私有变量
        /// </summary>
        private static List<FileExtInfo> fileExtList = new List<FileExtInfo>();

        private static List<FileInfo> filelist = new List<FileInfo>();

        #endregion

        #region 方法

        public static IEnumerable<string> ReadLines(string path)
        {
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 0x1000, FileOptions.SequentialScan))
            using (var sr = new StreamReader(fs, Encoding.UTF8))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    yield return line;
                }
            }
        }

        public static void DeleteFilesInFolderIfExists(string folderPath, string fileNameWithoutExtension)
        {
            var directory = new DirectoryInfo(folderPath);
            var tempUserProfileImages = directory.GetFiles(fileNameWithoutExtension + ".*", SearchOption.AllDirectories).ToList();
            foreach (var tempUserProfileImage in tempUserProfileImages)
            {
                FileHelper.DeleteIfExists(tempUserProfileImage.FullName);
            }
        }

        /// <summary>
        /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
        /// </summary>
        /// <param name="path">文件夹路径</param>
        /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
        /// <returns></returns>
        public static List<FileInfo> GetFiles(string path, string extName)
        {
            GetDir(path, extName);
            return filelist;
        }

        /// <summary>
        /// 私有方法,递归获取指定类型文件,包含子文件夹
        /// </summary>
        /// <param name="path"></param>
        /// <param name="extName"></param>
        private static void GetDir(string path, string extName)
        {
            try
            {
                string[] dir = Directory.GetDirectories(path); //文件夹列表   
                DirectoryInfo fdir = new DirectoryInfo(path);
                FileInfo[] file = fdir.GetFiles();
                //FileInfo[] file = Directory.GetFiles(path); //文件列表   
                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
                {
                    foreach (FileInfo f in file) //显示当前目录所有文件   
                    {
                        if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                        {
                            filelist.Add(f);
                        }
                    }
                    foreach (string d in dir)
                    {
                        GetDir(d, extName); //递归   
                    }
                }
            }
            catch (Exception ex)
            {
                //Utils.WriteErrorLog(ex);
                throw ex;
            }
        }

        /// <summary>
        /// 递归 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
        /// </summary>
        /// <param name="path">文件夹路径</param>
        /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
        /// <param name="parent"></param>
        /// <param name="parentLevel"></param>
        /// <returns></returns>
        public static List<FileExtInfo> GetAllFiles(string path, string extName, FileExtInfo parent = null, int parentLevel = 0)
        {
            try
            {
                string[] dir = Directory.GetDirectories(path); //文件夹列表   
                DirectoryInfo fdir = new DirectoryInfo(path);
                FileInfo[] files = fdir.GetFiles();
                //FileInfo[] file = Directory.GetFiles(path); //文件列表   
                if (files.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
                {
                    parentLevel++; //层级加一

                    //循环文件
                    foreach (FileInfo f in files) //显示当前目录所有文件   
                    {
                        if (string.IsNullOrWhiteSpace(extName) || extName == "*" || extName == "*.*" || extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                        {
                            var file = new FileExtInfo
                            {
                                filename = f.Name,
                                //file = f,
                                id = Guid.NewGuid(),
                                level = parentLevel,
                                parentid = parent == null ? Guid.Empty : parent.id,
                                type = EnumFileType.file
                            };
                            fileExtList.Add(file);
                        }
                    }

                    //循环目录
                    foreach (string d in dir)
                    {
                        if (!string.IsNullOrEmpty(d) && d.LastIndexOf("\\") > -1)
                        {
                            var fileinfo = new FileExtInfo
                            {
                                filename = d.Substring(d.LastIndexOf("\\")),
                                //file = d,
                                id = Guid.NewGuid(),
                                level = parentLevel,
                                parentid = parent == null ? Guid.Empty : parent.parentid,
                                type = EnumFileType.dir
                            };

                            //添加到list中
                            fileExtList.Add(fileinfo);

                            GetAllFiles(d, extName, fileinfo, parentLevel); //递归
                        }
                    }
                }
                return fileExtList;
            }
            catch (Exception ex)
            {
                //Utils.WriteErrorLog(ex);
                throw ex;
            }
        }

        #endregion
    }
}
